﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;


namespace DigitalPublisher
{
    class RegexClass
    {

        public List<string> listNamaImage = new List<string>();
        public List<string> listPathImage = new List<string>();
        public List<string> listPathBody = new List<string>();
        public List<string> listBodyPage = new List<string>();
        public List<string> listNamaScript = new List<string>();
        public List<string> listPathScript = new List<string>();
        public List<string> listNamaStyle = new List<string>();
        public List<string> listPathStyle = new List<string>();
        public List<string> listHeadPage = new List<string>();

        public string stylefile;
        public string scriptfile;
        public string scriptlinkfileUp;
        public string scriptlinkfileDown;

        public void HeadRegex(string isi)
        {
            string HeadTag = @"(?m)<head[^>]*>(\w|\W)*?</head[^>]*>";
            string strReg = @"</?head[^>]*?>";
            string strMatchScript = string.Empty;


            MatchCollection matchList = Regex.Matches(isi, HeadTag, RegexOptions.IgnoreCase);
            StringBuilder strbScriptList = new StringBuilder();
            foreach (Match matchSingleScript in matchList)
            {
                string strSingleScriptText = Regex.Replace(matchSingleScript.Value, strReg, string.Empty, RegexOptions.IgnoreCase);
                listHeadPage.Add(strSingleScriptText);
            }

        }

        public void BodyRegex(string isi)
        {
            string BodyTag = @"(?m)<body[^>]*>(\w|\W)*?</body[^>]*>";
            string strReg = @"</?body[^>]*?>";
            string strMatchScript = string.Empty;
            

            MatchCollection matchList = Regex.Matches(isi, BodyTag, RegexOptions.IgnoreCase);
            StringBuilder strbScriptList = new StringBuilder();
            foreach (Match matchSingleScript in matchList)
            {
                string strSingleScriptText = Regex.Replace(matchSingleScript.Value, strReg, string.Empty, RegexOptions.IgnoreCase);
                listPathBody.Add(strSingleScriptText);
            }

        }

        public void StyleRegex(string isi)
        {
            string strRegexStyle = @"(?m)<style[^>]*>(\w|\W)*?</style[^>]*>";
            string strRegex = @"<[^>]*>";
            string strMatchStyle = string.Empty;
            MatchCollection matchList = Regex.Matches(isi, strRegexStyle, RegexOptions.IgnoreCase);
            StringBuilder strStyleList = new StringBuilder();
            foreach (Match matchSingleStyle in matchList)
            {
                string strSingleStyleText = Regex.Replace(matchSingleStyle.Value, strRegex, string.Empty, RegexOptions.IgnoreCase);
                strStyleList.Append(strSingleStyleText + "\r\n");
            }
            stylefile += strStyleList.ToString();
        }

        public void ScriptRegex(string isi)
        {
            string strRegexScript = @"(?m)<script[^>]*>(\w|\W)*?</script[^>]*>";
            string strRegex = @"<[^>]*>";
            string strMatchScript = string.Empty;
            MatchCollection matchList = Regex.Matches(isi, strRegexScript, RegexOptions.IgnoreCase);
            StringBuilder strbScriptList = new StringBuilder();
            foreach (Match matchSingleScript in matchList)
            {
                string strSingleScriptText = Regex.Replace(matchSingleScript.Value, strRegex, string.Empty, RegexOptions.IgnoreCase);
                strbScriptList.Append(strSingleScriptText + "\r\n");
            }
            scriptfile += strbScriptList.ToString();
        }

        public void ImgRegex(string isi)
        {
            string imageTag = "<img.+?src=[\"'](.+?)[\"'].+?>";
            string imgSrc = "src=[\"'](.+?)[\"'].+?";
            string matchString = string.Empty;
            MatchCollection listImageTag = Regex.Matches(isi, imageTag, RegexOptions.IgnoreCase);
            StringBuilder strbImageList = new StringBuilder();

            foreach (Match match in listImageTag)
            {
                //strSingleScriptText2 = Regex.Replace(matchSingleImage.Value, strRegImg, "src=\"data:", RegexOptions.IgnoreCase);                
                strbImageList.Append(match);
                //Console.WriteLine(matchSingleImage);
            }

            MatchCollection listImageSrc = Regex.Matches(strbImageList.ToString(), imgSrc, RegexOptions.IgnoreCase);
            foreach (Match matchSingleImage in listImageSrc)
            {
                //Console.WriteLine(matchSingleImage);
                string[] word = matchSingleImage.ToString().Split('"');
                string[] nama = word[1].Split('/');
                listPathImage.Add(word[1].ToString());
                listNamaImage.Add(nama.Last());
            }

        }

        public void LinkRegex(string isi)
        {
            string strRegexStyle = "<link.+?href=[\"'](.+?)[\"'].+?>";
            string strRegex = "href=[\"'](.+?)[\"'].+?";
            string strMatchStyle = string.Empty;
            MatchCollection matchList = Regex.Matches(isi, strRegexStyle, RegexOptions.IgnoreCase);
            StringBuilder strScriptList = new StringBuilder();
            foreach (Match matchSingleStyle in matchList)
            {
                //string strSingleStyleText = Regex.Replace(matchSingleStyle.Value, strRegex, string.Empty, RegexOptions.IgnoreCase);
                strScriptList.Append(matchSingleStyle);
            }


            MatchCollection listImageSrc = Regex.Matches(strScriptList.ToString(), strRegex, RegexOptions.IgnoreCase);
            foreach (Match matchSingleImage in listImageSrc)
            {
                //Console.WriteLine(matchSingleImage);
                string[] word = matchSingleImage.ToString().Split('"');
                string[] nama = word[1].Split('/');
                listPathStyle.Add(word[1].ToString());
                listNamaStyle.Add(nama.Last());
            }
            
        }

        public void LinkScriptRegex(string isi)
        {
            string strRegexLinkScript = "<script.+?src=[\"'](.+?)[\"'].+?>";
            string strRegex = "src=[\"'](.+?)[\"'].+?";
            string strMatchStyle = string.Empty;
            MatchCollection matchList = Regex.Matches(isi, strRegexLinkScript, RegexOptions.IgnoreCase);
            StringBuilder strScriptList = new StringBuilder();
            foreach (Match matchSingleStyle in matchList)
            {
                //string strSingleStyleText = Regex.Replace(matchSingleStyle.Value, strRegex, string.Empty, RegexOptions.IgnoreCase);
                strScriptList.Append(matchSingleStyle);
            }


            MatchCollection listImageSrc = Regex.Matches(strScriptList.ToString(), strRegex, RegexOptions.IgnoreCase);
            foreach (Match matchSingleImage in listImageSrc)
            {
                //Console.WriteLine(matchSingleImage);
                string[] word = matchSingleImage.ToString().Split('"');
                string[] nama = word[1].Split('/');
                listPathScript.Add(word[1].ToString());
                listNamaScript.Add(nama.Last());
            }

        }
        
    }
}
